package com.donleo.stmg.entity.service.impl;

import com.donleo.stmg.common.api.CommonResult;
import com.donleo.stmg.entity.model.Permission;
import com.donleo.stmg.entity.model.User;
import com.donleo.stmg.entity.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donleo.stmg.entity.service.RedisService;
import com.donleo.stmg.entity.service.UserService;
import com.donleo.stmg.entity.vo.LoginParams;
import com.donleo.stmg.entity.vo.UserVo;
import com.donleo.stmg.relation.mapper.UserTeamRelationMapper;
import com.donleo.stmg.relation.model.UserTeamRelation;
import com.donleo.stmg.utils.CommonPath;
import com.donleo.stmg.utils.DateUtil;
import com.donleo.stmg.utils.jwt.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private RedisService redisService;

    @Autowired
    private UserTeamRelationMapper userTeamRelationMapper;

    @Override
    public User getUserByCode(String code) {
        List<User> users = userMapper.getUserByCode(code);
        Assert.isTrue(users.size() == 1, "您输入的账户不存在，或者有多个相同的账户");
        return users.get(0);
    }

    @Override
    public List<Permission> getPermissionsByUserId(String userCode) {
        return userMapper.getPermissionsByUserId(userCode);
    }

    @Override
    public String login(LoginParams loginParams) {
        String code = loginParams.getCode();
        Assert.notNull(code, "账号必须不能为空");
        String password = loginParams.getPassword();
        Assert.notNull(password, "密码必须不能为空");
        User userByCode = getUserByCode(code);
        //判断用户输入的密码与数据库中查出来的密码是否相等
        boolean matches = passwordEncoder.matches(password, userByCode.getPassword());
        //如果密码相等，表明用户信息输入正确，生成一个token令牌，否则返回null
        if (matches) {
            return jwtTokenUtil.generateToken(userByCode);
        }
        return null;
    }

    @Override
    public boolean isValidateCode(String validateCode) {
        String backValidateCode = (String) redisService.get("backValidateCode");
        return !backValidateCode.equals(validateCode);
    }

    @Override
    public Boolean register(User user) {
        String code = user.getCode();
        Assert.notNull(code, "用户编号不能为空");
        String password = user.getPassword();
        Assert.notNull(password, "密码不能为空");
        List<User> users = userMapper.getUserByCode(code);
        if (users.size() >= 1) {
            return false;
        }
        user.setPassword(passwordEncoder.encode(password));
        user.setCreateTime(new Date());
        user.setDataEnable(CommonPath.YES.getValue());
        userMapper.insert(user);
        return true;
    }

    @Override
    public User loadUserAuthorityByUserCode(String userCode) {
        boolean b = redisService.hasKey("user::pmsAuthors:" + userCode);
        User user;
        if (b) {
            user = (User) redisService.get("user::pmsAuthors:" + userCode);
        } else {
            List<User> users = userMapper.getUserByCode(userCode);
            user = users.get(0);

            List<Permission> permissionList = userMapper.getPermissionsByUserId(user.getCode());
            //获取用户拥有的权限
            HashSet<Permission> permissions = new HashSet<>(permissionList);
            user.setAuthorities(permissions);
            redisService.set("user::pmsAuthors:" + userCode, user,30*60);
        }
        return user;
    }

    @Override
    public void frozenUser(User user) {
        userMapper.updateById(user);
    }

    @Override
    public boolean isFrozenUser(String code) {
        List<User> userList = userMapper.getUserByCode(code);
        User user = userList.get(0);
        return user.getDataEnable().equals(CommonPath.NO.getValue());
    }

    @Override
    public List<User> findAll(UserVo userVo) {
        return userMapper.findAll(userVo);
    }

    @Override
    public long getCount(UserVo userVo) {
        return userMapper.getCount(userVo);
    }

    @Override
    public CommonResult updateUser(User user) {
        user.setUpdateTime(new Date());
        int i = userMapper.updateById(user);
        if (i > 0) {
            return CommonResult.success(i);
        } else {
            return CommonResult.failed();
        }
    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }

    @Override
    public List<User> findUserByRoleCode(String roleCode) {
        return userMapper.findUserByRoleCode(roleCode);
    }

    @Override
    public List<User> findUserByTeamCode(String teamCode, Integer pageNum, Integer pageSize) {
        return userMapper.findByTeam(teamCode, pageNum, pageSize);
    }

    @Override
    public long getCountByTeamCode(String teamCode) {
        return userMapper.getCountByTeamCode(teamCode);
    }

    @Override
    public List<User> findUngroupedUser() {

        return userMapper.findUngroupedUser();
    }
}
